from splash_screen import TitleScreen
from panda3d.core import Vec2,Point3,Vec3,Vec4
from marioIcon import MarioIcon
from direct.interval.IntervalGlobal import *

class Overworld:	
	'''
		Creates a splash screen
	'''
	def __init__(self, parent):
		self.parent = parent
		self.__loadScreen()
		self.__set2DProps()
		
		self.currentNode = 0;
		
		self.marioIcon = MarioIcon(parent)
		self.marioIcon.model.setPos(Point3(245, 130, 0))

	'''
		Loads the textures for the screens
	'''
	def __loadScreen(self):
		self.backgroundTex = self.parent.loader.loadTexture("models/levels/menu_items/MarioOverworld.png")
		self.backgroundModel = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundModel.setTexture(self.backgroundTex)
		
	'''
		Sets the properties for a two dimensional figure
	'''
	def __set2DProps(self):
		self.backgroundModel.setHpr(Vec3(0,-90,0))
		self.backgroundModel.setScale(self.parent.WIN_WIDTH, self.parent.BLOCK_SIZE, self.parent.WIN_HEIGHT)
		self.backgroundModel.setPos(self.parent.WIN_WIDTH/2.0, self.parent.WIN_HEIGHT/2.0,0)
		
		# maximize efficiency
		self.backgroundModel.setBin("unsorted", 0)
		self.backgroundModel.setDepthTest(False)
		self.backgroundModel.setTransparency(1)

		self.backgroundModel.reparentTo(render)
		
	'''
		Places this object on the screen
	'''
	def show(self):
		#self.parent.level.hide()
		self.backgroundModel.show()
		self.marioIcon.show()
		self.setupInterrupts()
		
	'''
		Hides the title screen
	'''
	def hide(self):
		self.backgroundModel.hide()
		self.marioIcon.hide()
		
	'''
		Set's up the enter key to take you to the correct world
	'''
	def setupInterrupts(self):
		self.parent.accept("enter", self.enterLevel)
		self.parent.accept("w", self.moveIcon, ["up"])
		self.parent.accept("s", self.moveIcon, ["down"])
		self.parent.accept("a", self.moveIcon, ["left"])
		self.parent.accept("d", self.moveIcon, ["right"])
		
	def enterLevel(self):
		if(self.currentNode ==  2 or self.currentNode >= 4):
			self.parent.level.loadLevel(self.getCurrentLevel())
			self.parent.ignore("enter")#note: we need to re-accept enter event 
			#whenever the overworld is shown again
			self.hide()
			self.parent.level.mario.startWalkingTask()
		
	'''
		Retrieves the current level to load the correct level
	'''
	def getCurrentLevel(self):
		return self.currentLevel 
		
	'''
		Moves mario icon to correct position in overworld.
		
		Note: if mario is on a level space, self.currentLevel has been set appropriately
	'''
	def moveIcon(self, direction):
		#node 0 == start square
		#node 1,2,3 == vertical upwards from start
		#node 2 is lvl 1
		#node 4 is lvl 2 
		#node 5 is lvl 3
		
		node0 = Point3(245, 130, 0)
		node1 = Point3(245, 190, 0)
		node2 = Point3(245, 270, 0)
		node3 = Point3(245, 340, 0)
		node4 = Point3(315, 270, 0)
		node5 = Point3(315, 340, 0)
		
		if(direction == "up"):
			if(self.currentNode == 0):
				self.marioIcon.model.posInterval(.5,node1).start()
				self.currentNode = 1
			elif(self.currentNode == 1):
				self.marioIcon.model.posInterval(.5,node2).start()
				self.currentNode = 2
				self.currentLevel = 'level_one'
			elif(self.currentNode == 2):
				self.marioIcon.model.posInterval(.5,node3).start()
				self.currentNode = 3
		elif(direction == "down"):
			if(self.currentNode == 3):
				self.marioIcon.model.posInterval(.5,node2).start()
				self.currentNode = 2
				self.currentLevel = 'level_one'
			elif(self.currentNode == 2):
				self.marioIcon.model.posInterval(.5,node1).start()
				self.currentNode = 1
			elif(self.currentNode == 1):
				self.marioIcon.model.posInterval(.5,node0).start()
				self.currentNode = 0
		elif(direction == "left"):
			if(self.currentNode == 4):
				self.marioIcon.model.posInterval(.5,node2).start()
				self.currentNode = 2
				self.currentLevel = 'level_one'
			elif(self.currentNode == 5):
				self.marioIcon.model.posInterval(.5,node3).start()
				self.currentNode = 3
		else:
			if(self.currentNode == 2):
				self.marioIcon.model.posInterval(.5,node4).start()
				self.currentNode = 4
				self.currentLevel = 'level_two'
			elif(self.currentNode == 3):
				self.marioIcon.model.posInterval(.5,node5).start()
				self.currentNode = 5
				self.currentLevel = 'level_three'
		
	'''
		Replaces the mario sprite and the background with a game over screen
	'''
	def showGameOver(self):
		self.backgroundTex = self.parent.loader.loadTexture("models/levels/menu_items/GameOver.png")
		self.parent.level.mario.model.setPos(2,2,0)
		self.backgroundModel.setTexture(self.backgroundTex)
		self.marioIcon.hide()
		self.parent.ignore("enter")
		self.parent.ignore("w")
		self.parent.ignore("s")
		self.parent.ignore("a")
		self.parent.ignore("d")
		self.parent.themeMusic.stop();
		
	'''
		Replaces the mario sprite and the background with a you win screen
	'''
	def showYouWin(self):
		self.backgroundTex = self.parent.loader.loadTexture("models/levels/menu_items/YouWin.png")
		self.parent.level.mario.model.setPos(2,2,0)
		self.backgroundModel.setTexture(self.backgroundTex)
		self.marioIcon.hide()
		self.parent.ignore("enter")
		self.parent.ignore("w")
		self.parent.ignore("s")
		self.parent.ignore("a")
		self.parent.ignore("d")
		self.parent.themeMusic.stop();
		







